Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement the Unwrap method for custom error types #2857

Merged
merged 1 commit into from
Aug 27, 2024

Conversation

favonia
Copy link
Contributor

@favonia favonia commented Aug 11, 2024

Description

Since the introduction of error wrapping in Go 1.13, more and more Go errors implement the Unwrap method to work with the new error checking paradigm using errors.{Is,As}. This PR is to implement such an Unwrap method for custom error types defined in this library that wrap around a private Error. By implementing Unwrap that returns the private Error, these custom errors will be automatically seen by the Go standard library as wrappers of the private Error. Here's some sample code enabled by this PR:

var cloudflareErr cloudflare.Error
if errors.As(err, &cloudflareErr) {
	fmt.Printf("HTTP status code: %d", cloudflareErr.StatusCode)
}

This is extremely useful when trying to distinguish "errors from the Cloudflare API itself" and "errors due to other reasons (e.g., network errors)". Also, for reasons, only cloudflare.Error implements the ErrorMessageContains method. In sum, the Unwrap function gives users a clean interface to retrieve the private Error that works very well with the current Go standard library.

Has your change been tested?

Not yet. I'm worried a test involving errors.As would not run on platforms using older Go compilers.

Types of changes

What sort of change does your code introduce/modify?

  • New feature (non-breaking change which adds functionality)

Checklist:

  • My code follows the code style of this project.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@favonia favonia requested a review from jacobbednarz as a code owner August 11, 2024 13:11
Copy link
Contributor

github-actions bot commented Aug 11, 2024

changelog detected ✅

@favonia favonia changed the title implement the Unwrap method for error types implement the Unwrap method for custom error types Aug 11, 2024
@favonia
Copy link
Contributor Author

favonia commented Aug 12, 2024

I now see that the 2.x library already has all the goodies. Perhaps this PR can be seen as an attempt to make 0.x more usable before completely abandoning it.

@jacobbednarz
Copy link
Member

jacobbednarz commented Aug 27, 2024

when it comes to the Cloudflare errors, you really don't want to be using Unwrap as it is here since it may contain multiple errors and you need to handle that in something like Error() to get the nicer formatting/handling (which could always be ported over but isn't what we're doing here). i.e. https://github.com/cloudflare/terraform-provider-cloudflare/blob/b33577b3af8207bc41b5ab4ae3ffe66f53ee8ff7/internal/sdkv2provider/resource_cloudflare_access_keys_configuration.go#L57-L64

we can add this here, however, going forward, we limiting what we do in 0.x as it detracts effort from the automatically generated versions.

@jacobbednarz jacobbednarz merged commit e4ee7f2 into cloudflare:master Aug 27, 2024
10 checks passed
@github-actions github-actions bot added this to the v0.103.0 milestone Aug 27, 2024
@favonia favonia deleted the unwrap-errors branch August 27, 2024 16:02
Copy link
Contributor

This functionality has been released in v0.103.0.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants